package com.ruoyi.license.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig implements InitializingBean {

    @Value("${swagger.host:{null}}")
    private String host;

    @Value("${swagger.title:{null}}")
    private String title;

    @Value("${swagger.version:{null}}")
    private String version;

    @Value("${swagger.description:{null}}")
    private String description;

    @Value("${swagger.license:{null}}")
    private String license;

    @Value("${swagger.licenseUrl:{null}}")
    private String licenseUrl;

    @Value("${swagger.contact.name:{null}}")
    private String contactName;

    @Value("${swagger.contact.email:{null}}")
    private String contactEmail;

    @Value("${swagger.contact.url:{null}}")
    private String contactUrl;

    @Value("${swagger.package:{null}}")
    private String packagePath;

    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(packagePath)).build().host(host);
    }

    private ApiInfo apiInfo() {

        //作者信息
        Contact contact = new Contact(contactName, contactUrl, contactEmail);
        return new ApiInfo(title, description, version, contactUrl, contact, license, licenseUrl, new ArrayList<>());
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("SwaggerConfig==================afterPropertiesSet");
    }
}
